package com.bwsoft.api.service.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import com.bwsoft.admin.domain.Account;
import com.bwsoft.admin.domain.Report;
import com.bwsoft.admin.domain.Standard;
import com.bwsoft.api.pojo.ResponsePojo;
import com.bwsoft.api.service.StandService;
import com.bwsoft.api.util.Constant;
import com.bwsoft.util.Base64Util;
import com.bwsoft.util.DesUtil;
@Service(StandService.NAME)
public class StandServiceImpl extends BaseServiceImpl implements StandService{
	private static Logger LOG = Logger.getLogger(StandServiceImpl.class);

	
	/**
	 * 对应企业的报告或者是
	 * 
	 */
	@Override
	public ResponsePojo getList(Integer standardField, Integer standardType,
			String search, Integer page) {
		
		ResponsePojo rp = new ResponsePojo();
		// 判断验证码是否正确
		try {
			
			
			
			String hql="from Standard as s where s.status=1 and s.displayStatus=1 ";
			
			if(standardField!=null){
				hql+= " and s.field="+standardField;
			}
			if(standardType!=null){
				hql+= " and s.type="+standardType;
			}
			
			if(search!=null && !search.trim().equals("")){
				hql+=" and s.standardName like '%"+search+"%'";
			}
			
			hql+=" order by s.addTime desc";
			LOG.info("组装的HQL ：" +hql);
			
			
			List<Standard> totalCount=standardDao.findByHql(hql.toString());
			List<Standard> standards=standardDao.findPage(hql.toString(), Constant.PAGE_COUNT , (page-1)*Constant.PAGE_COUNT);
			LOG.info("按照条件可以查出"+totalCount.size()+"条数据");
			LOG.info("查询出"+standards.size()+"条数据");
			
			
			
			Map<String ,Object> returnMap=new HashMap();
			returnMap.put("pageNow", page);
			returnMap.put("totalPage", totalCount.size()/Constant.PAGE_COUNT+1);
			returnMap.put("standard", createStandard(standards));
			rp.setErrcode(0);
			rp.setErrmsg("操作成功！");
			rp.setData(returnMap);
		} catch (Exception e) {
			e.printStackTrace();
			LOG.info("异常" + e);
			rp.setErrcode(50000);
			rp.setErrmsg("服务器异常");
			rp.setData(e.toString());
			return rp;
		}
		return rp;
		
	}
	
	
	private List<Map<String,Object>> createStandard(List<Standard> standards){
		List<Map<String,Object>> returnMap=new ArrayList<Map<String,Object>>();
		Map<String, String> fieldMap=dictionaryDataService.getDicDataByName("standard_field");
		Map<String, String> typeMap=dictionaryDataService.getDicDataByName("standard_type");
		for (Standard standard : standards) {
			
			Map<String,Object> map=new HashMap<String, Object>();
			
			map.put("id", standard.getStandardId());
			map.put("field", fieldMap.containsKey(standard.getField().toString())?fieldMap.get(standard.getField().toString()):"" );
			map.put("type",fieldMap.containsKey(standard.getType().toString())?fieldMap.get(standard.getType().toString()):"");
			map.put("title", standard.getStandardName());
			map.put("date", new SimpleDateFormat("yyyy-MM-dd").format(standard.getAddTime()));
			try {
				map.put("fileUrl", "/api/jsp/pdf.jsp?r="+Base64Util.getBase64(DesUtil.encrypt("fileid="+standard.getFile().getId(), Constant.KEY_DES).getBytes("utf-8")));
			} catch (Exception e) {
				e.printStackTrace();
				LOG.info("组装文件路径异常"+e);
				map.put("fileUrl", "/api/jsp/pdferror.jsp?");
			}
			returnMap.add(map);
		}
		
		return returnMap;
	}

}
